<?php

namespace common\models;

use common\components\active\ActiveRecord;
use yii\behaviors\TimestampBehavior;

/**
 * This is the model class for table "enterprise_case".
 *
 * @property int          $enterprise_case_id
 * @property int          $enterprise_case_enterprise_id 企业id
 * @property int          $enterprise_case_status        案件状态
 * @property int          $enterprise_case_category_id   分类
 * @property int          $enterprise_case_created_at    创建时间
 * @property int          $enterprise_case_updated_at    更新时间
 * @property int          $enterprise_case_user_id       创建人id
 * @property string       $enterprise_case_sn
 * @property int          $enterprise_case_accept_at     受理时间
 * @property int          $enterprise_case_finish_at     完成时间
 *
 * @property CaseCategory $enterpriseCaseCategory
 * @property Enterprise   $enterpriseCaseEnterprise
 * @property Proposer     $proposer
 */
class EnterpriseCase extends ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'enterprise_case';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['enterprise_case_enterprise_id'], 'required'],
            [
                [
                    'enterprise_case_enterprise_id',
                    'enterprise_case_status',
                    'enterprise_case_category_id',
                    'enterprise_case_created_at',
                    'enterprise_case_updated_at',
                    'enterprise_case_user_id',
                    'enterprise_case_accept_at',
                    'enterprise_case_finish_at'
                ],
                'integer'
            ],
            [['enterprise_case_sn'], 'string', 'max' => 45],
            [
                ['enterprise_case_category_id'],
                'exist',
                'skipOnError'     => true,
                'targetClass'     => CaseCategory::class,
                'targetAttribute' => ['enterprise_case_category_id' => 'case_category_id'],
                'message' => '错误的分类id'
            ],
            [
                ['enterprise_case_enterprise_id'],
                'exist',
                'skipOnError'     => true,
                'targetClass'     => Enterprise::class,
                'targetAttribute' => ['enterprise_case_enterprise_id' => 'enterprise_id'],
                'message' => '无效的企业id'
            ],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'enterprise_case_id'            => 'Enterprise Case ID',
            'enterprise_case_enterprise_id' => '企业id',
            'enterprise_case_status'        => '案件状态',
            'enterprise_case_category_id'   => '分类',
            'enterprise_case_created_at'    => '创建时间',
            'enterprise_case_updated_at'    => '更新时间',
            'enterprise_case_user_id'       => '创建人id',
            'enterprise_case_sn'            => 'Enterprise Case Sn',
            'enterprise_case_accept_at'     => '受理时间',
            'enterprise_case_finish_at'     => '完成时间',
        ];
    }

    public function behaviors()
    {
        return [
            [
                'class'              => TimestampBehavior::class,
                'createdAtAttribute' => 'enterprise_case_created_at',
                'updatedAtAttribute' => 'enterprise_case_updated_at',
            ],
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getEnterpriseCaseCategory()
    {
        return $this->hasOne(CaseCategory::class, ['case_category_id' => 'enterprise_case_category_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getEnterpriseCaseEnterprise()
    {
        return $this->hasOne(Enterprise::class, ['enterprise_id' => 'enterprise_case_enterprise_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getProposer()
    {
        return $this->hasOne(Proposer::class, ['proposer_ enterprise_case_id' => 'enterprise_case_id']);
    }
}
